package com.baseproject.datasource.local.file;

import android.content.Context;

import com.anupcowkur.reservoir.ReservoirClearCallback;
import com.anupcowkur.reservoir.ReservoirDeleteCallback;
import com.anupcowkur.reservoir.ReservoirGetCallback;
import com.baseproject.json.JsonUtils;
import com.baseproject.util.FileUtils;
import com.baseproject.util.PreferencesUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by hp on 2017/6/14.
 */

public class MyLocalFileStrategy implements ILocalFileStrategy {

    public MyLocalFileStrategy() {
    }

    @Override
    public Observable<File> getFile(String destDir, String destFileName) {
        return FileUtils.getFile(destDir, destFileName);
    }

    @Override
    public File saveFile(InputStream is, String path, String fileName) {
        return FileUtils.writeInputStreamToDisk(is, path, fileName);
    }


    @Override
    public void init(Context context, long maxSize) throws IOException {

    }

    @Override
    public boolean contains(String key) throws IOException {
        return false;
    }

    @Override
    public void put(String key, Object object) throws IOException {

    }

    @Override
    public Observable<Boolean> putUsingObservable(String key, Object object) {
        return Observable.create(new Observable.OnSubscribe<Boolean>() {
            @Override
            public void call(Subscriber<? super Boolean> subscriber) {
                PreferencesUtils.setString(key, object.toString());
            }
        });
    }

    @Override
    public <T> T get(String key, Class<T> classOfT) throws IOException {
        return null;
    }

    @Override
    public <T> T get(String key, Type typeOfT) throws IOException {
        return null;
    }

    @Override
    public <T> void getAsync(String key, Class<T> classOfT, ReservoirGetCallback<T> callback) {

    }

    @Override
    public <T> void getAsync(String key, Type typeOfT, ReservoirGetCallback<T> callback) {

    }

    @Override
    public <T> Observable<T> getUsingObservable(String key, Class<T> className) {
        return PreferencesUtils.getString(key, "")
                .map(new Func1<String, T>() {
                    @Override
                    public T call(String string) {
                        T localObject = (T) JsonUtils.getInstance().strToModel(string, className);
                        return localObject;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public <T> Observable<T> getUsingObservable(String key, Class<T> classOfT, Type typeOfT) {
        return null;
    }

    @Override
    public void delete(String key) throws IOException {

    }

    @Override
    public void deleteAsync(String key, ReservoirDeleteCallback callback) {

    }

    @Override
    public Observable<Boolean> deleteUsingObservable(String key) {
        return null;
    }

    @Override
    public void clear() throws IOException {

    }

    @Override
    public void clearAsync(ReservoirClearCallback callback) {

    }

    @Override
    public Observable<Boolean> clearUsingObservable() {
        return null;
    }
}
